﻿using System;
using Grundy.Common;
using Grundy.CryptographicServices;
using Grundy.Entity;
using Grundy.Message;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NSubstitute;
using System.Linq;

namespace Grundy.Licensing.Server.Tests
{
    [TestClass]
    public class LicenseStoreTests
    {
        private LicenseManager licenseManager;
        private IdentifierCollection selfIdentifiers;

        [TestInitialize]
        public void Setup()
        {
            licenseManager = Substitute.For<LicenseManager>();
            selfIdentifiers = new NodeFingerprintIdentityProvider().GetIdentifiers();
            licenseManager.CreateCustomerEntitlementFromLicense(null).ReturnsForAnyArgs(GetCustomerEntitlement());
        }

        [TestMethod]  
        public void Should_return_self_entitlements()
        {
            var licenseStore = new LicenseStore(licenseManager, new NodeIdentityComparer(selfIdentifiers), new PassThroughProvider());
            licenseStore.Add(new Container().Serialize());
            Assert.AreEqual(1, licenseStore.SelfCustomerProductLicenseEntitlements.Count());
        }

        [TestMethod]
        public void Should_return_other_entitlements()
        {
            var licenseStore = new LicenseStore(licenseManager, new NodeIdentityComparer(selfIdentifiers), new PassThroughProvider());
            licenseStore.Add(new Container().Serialize());
            Assert.AreEqual(1, licenseStore.OtherCustomerProductLicenseEntitlements.Count());
        }

        private CustomerEntitlement GetCustomerEntitlement()
        {
            return new CustomerEntitlement
                       {
                           Id = Guid.NewGuid(),
                           Customer = new Customer { Id = Guid.NewGuid(), Name = "Test Customer" },
                           LicenseEntitlements = new LicenseEntitlementCollection
                                                 {
                                                    GetLicenseEntitlement(true),
                                                    GetLicenseEntitlement(false)
                                                 }
                       };
        }

        private LicenseEntitlement GetLicenseEntitlement(bool self)
        {
            var identifierCollection = new IdentifierCollection(selfIdentifiers);
            if (!self)
            {
                identifierCollection[Identifiers.Processor].First().Value = new Random().Next().ToString();
            }

            return new LicenseEntitlement
                       {
                           License = new UncountedFloatingLicense { Client = LicenseClientType.Machine, Feature = new Feature { Id = Guid.Empty, Name = "Feature1" } },
                           Node = new Node { Name = "Node " + new Random().Next(), Identifiers = identifierCollection },
                           Product = new Product { Name = "Product1"}
                       };
        }
    }
}